import Storage from './storage';
const storage = new Storage();
const _findApply = Symbol("_findApply");
export default class FriendObject {
    static instance = null;
    #friendList = [];                                                   //好友列表
    #applyList = [];                                                    //好友申请列表
    constructor() {
        if (!FriendObject.instance) { FriendObject.instance = this }
        this.member_id = 0;
        this.port = '';
        this.sertype = '';
        return FriendObject.instance;
    }
    /*处理申请*/
    resolveAgree(clientMsgId) {
        const data = this[_findApply](clientMsgId);
        if (data.code != 200) { return { code: 301, data: data.data } }
        this.deletApply(data.data.index);
        this.initLocalStorage()
        return { code: 200, data: data.data }
    }
    /*删除申请*/
    deletApply(index) {
        if (index < 0 || index > this.#applyList.length) {
            return { code: 301, data: "删除失败！" }
        }
        this.#applyList.splice(index, 1);
        return { code: 200, data: "删除成功！" }
    }
    /*收到好友请求*/
    addApply(apply) {
        if (!apply) {
            console.log('收到好友请求错误！friendObject');
            return { code: 301, data: '收到好友请求错误！friendObject' }
        }
        for (let i = 0; i < this.#applyList.length; i++) {
            if (this.#applyList[i].head.fromMemberId == apply.head.fromMemberId) {
                this.#applyList.splice(i, 1);
                i--;
            }
        }
        this.#applyList.push(apply);
        this.initLocalStorage();
        return { code: 200, data: '成功！' }
    }
    /*获取好友申请列表*/
    getAppList() {
        return { code: 200, data: this.#applyList }
    }
    /*设置基本信息*/
    setProperty(data) {
        if (!data) { console.log('设置基本信息错误！friendObject'); return { code: 301, data: '设置基本信息错误！friendObject' } }
        this.member_id = data.member_id;
        this.sertype = data.sertype;
        this.port = data.port;
        return { code: 200, data: '成功！' }
    }
    /*本地存储*/
    initLocalStorage() {
        var key = `${this.member_id}~${this.sertype}~${this.port}-friend`;
        localStorage.setItem(key, JSON.stringify(this.#applyList));
        return { code: 200, data: '成功！' }
    }
    /*载入本地存储*/
    initApplyList() {
        let res = storage.getLocalStorage('applyList');                                    //载入本地存储
        console.log(res);
        if (res.code == 200) {
            this.#applyList = res.data;
        } else {
            this.#applyList = [];
        }
        res = storage.getLocalStorage('friendList');
        console.log(res);
        if (res.code == 200) { this.#friendList = res.data } else {
            this.#friendList = [];
        }
        return { code: 200, data: '成功！' }
    }
    /*添加好友*/
    addFriend(talk_id) {
        let person = this.#friendList.find((person) => {
            if (person == talk_id) { return person; }
        })
        if (person) {
            return { code: 301, data: "你们已经是好友！" }
        } else {
            this.#friendList.push(talk_id);
            storage.saveByLocalStorage('friendList', this.#friendList);
            return { code: 200, data: "添加成功！" }
        }
    }
    /*删除好友*/
    deleteFriend(talk_id) {
        let person = this.#friendList.find((person, index) => {
            if (person == talk_id) {
                this.#friendList.splice(index, 1);
                return person;
            }
        })
        if (person) {
            return { code: 301, data: "删除好友失败！改好友不存在！" }
        } else {
            storage.saveByLocalStorage('friendList', this.#friendList);
            console.log('删除好友')
            return { code: 200, data: "删除成功！" }
        }
    }
    /*查看好友列表*/
    getFriendList() {
        return { code: 200, data: this.#friendList }
    }
    /*清空本地属性*/
    clear() {
        this.member_id = '';
        this.sertype = '';
        this.port = '';
        return { code: 200, data: "成功！" }
    }
    /*离线消息队列里有好友请求*/
    addOffLineFriend(list) {
        var arr = list.filter(item => {
            if (item.head.action == 'addFriendUseMemberId' || item.head.action == 'addFriendUseTalkId') {
                this.#applyList.push(item);
                return item
            }
        })
        if (arr.length > 0) {
            return { code: 200, data: arr }
        }
        return { code: 301, data: '没有离线好友申请！' }
    }
    /*找到好友请求*/
    [_findApply](clientMsgId) {
        let applyIndex;
        const apply = this.#applyList.find((item, index) => {
            if (item.head.clientMsgId === clientMsgId) {
                applyIndex = index;
                return item
            }
        })
        if (!apply) { return { code: 301, data: "该好友申请不存在！" } }
        return { code: 200, data: apply, index: applyIndex }
    }
}